package servlet;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.MessageDao;

/**
 * Servlet implementation class LeaveMessageServlet
 */
@WebServlet("/LeaveMessageServlet")
public class LeaveMessageServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public LeaveMessageServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        this.doPost(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String message = request.getParameter("message");
        String uName = (String)request.getSession().getAttribute("uName");
        String mDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());

//		System.out.println("留言内容："+message);

        if(!message.equals("")) {
            new MessageDao().insert(uName, message, mDate);
            response.sendRedirect("MessageServlet");
        }else {
            //转发可能出现地址问题
            request.getSession().setAttribute("messageErrorMess", "您输入的留言内容为空！");
            response.sendRedirect("message.jsp");
        }
    }

}
